/**
 * 
 */
package net.javaonfly.core.servlet;

import java.io.IOException;
import java.io.Serializable;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.javaonfly.core.dispatch.Dispatcher;
import net.javaonfly.core.exception.JofException;

/**
 * @author Administrator
 * 
 */
public class JavaOnFlyServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8353925269875864860L;

	private Dispatcher dispatcher;

	@Override
	protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1)
			throws ServletException, IOException {
		processRequest(arg0, arg1);
	}

	@Override
	protected void doPost(HttpServletRequest arg0, HttpServletResponse arg1)
			throws ServletException, IOException {
		processRequest(arg0, arg1);
	}

	@Override
	protected void doPut(HttpServletRequest arg0, HttpServletResponse arg1)
			throws ServletException, IOException {
		processRequest(arg0, arg1);
	}

	@Override
	public void destroy() {
		super.destroy();
	}

	@Override
	public void init(ServletConfig servletConfig) throws ServletException {
		dispatcher = new Dispatcher(servletConfig
				.getInitParameter("CtrlPackage"));
	}

	private void processRequest(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			String url = request.getPathInfo();
			Map<String, Serializable> input = new HashMap<String, Serializable>();
			Enumeration ps = request.getParameterNames();
			while (ps.hasMoreElements()) {
				String parameterName = (String) ps.nextElement();
				String strVal = request.getParameter(parameterName);
				if (parameterName.startsWith("i")) {
					input.put(parameterName, Integer.parseInt(strVal));
				} else if (parameterName.startsWith("l")) {
					input.put(parameterName, Long.parseLong(strVal));
				} else if (parameterName.startsWith("f")) {
					input.put(parameterName, Float.parseFloat(strVal));
				} else if (parameterName.startsWith("a")) {
					input.put(parameterName, request
							.getParameterValues(parameterName));
				} else {
					input.put(parameterName, strVal);
				}
			}
			request.setAttribute("JofInput", input);
			Map<String, Object> output = new HashMap<String, Object>();
			request.setAttribute("JofOutput", output);
			Map<String, Object> session = (Map<String, Object>) request
					.getSession().getAttribute("JofSession");
			if (null == session) {
				session = new HashMap<String, Object>();
			}
			String path = dispatcher.dispatch(url, input, output, session,
					request, response);
			for (String key : output.keySet()) {
				request.setAttribute(key, output.get(key));
			}
			
			if (null != path) {
				RequestDispatcher render = request.getRequestDispatcher(path);
				if (render != null) {
					render.forward(request, response);
				} else {
					throw new JofException("Can not find the resource " + path);
				}
			}
		} catch (Exception e) {
			try {
				e.printStackTrace();
				response.sendError(500, e.getMessage());
			} catch (IOException e1) {
			}
		}

	}

}
